from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

'''基础配置'''
# 设置邮件服务器地址
server = 'smtp.163.com'
# 访问端口号
port = 25

# 发件人
sender = 'jingying0037@163.com'
# 授权码
code = 'AVNCNFWSUJQEALFA'
# 收件人
receivers = 'jingying0037@163.com;a15891502666@163.com;1070647994@163.com;hz20215211314@163.com;c1632531779@163.com;liangbing20002021@163.com'

'''创建邮件'''
# 创建邮件对象
mail = MIMEMultipart()
# 初始化发件人
mail['from'] = sender
# 添加收件人
mail['to'] = receivers
# 添加主题
mail['subject'] = 'Ranzhi自动化测试报告！'

'''添加附件'''
# 文件路径
path = r'selenium\ranzhi\report\report.html'
# 读取附件
with open(path,'rb') as file:
    report = file.read()
# 对附件进行编码
attachment = MIMEText(report,'base64','utf-8')
# 设置附件的类型
attachment['Content-Type'] = 'application/octet-stream'
# 设置附件的处理方式
attachment['Content-Disposition'] = 'attachment;filename=%s'%path.split('\\')[-1]
# 添加附件
mail.attach(attachment)

'''添加正文'''
# 创建正文
content = '''
<p>Dear Boss:</p>
    <p>This is the report you need.</p>

<p>Regards,</p>
<p>Tom</p>
'''
# 对正文进行编码
body = MIMEText(content,'html','utf-8')
# 添加正文
mail.attach(body)

'''发送邮件'''
# 创建SMTP对象
smtp = smtplib.SMTP()
# 连接服务器
smtp.connect(server,port)
# 登陆服务器
smtp.login(sender,code)
# 发送邮件
smtp.sendmail(sender,receivers.split(';'),mail.as_string())
# 关闭服务器
smtp.close()
print('邮件发送完毕！')